package com.markerhub.service.impl;

import com.markerhub.common.lang.Result;
import com.markerhub.mapper.CabinetMapper;
import com.markerhub.mapper.OrderMapper;
import com.markerhub.service.CabinetService;
import org.apache.ibatis.binding.BindingException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author by MaXiaofeng
 * @Description TODO
 * @Date 2022/2/22 21:03
 * @Version 1.0.0
 */
@Service
public class CabinetServiceImpl implements CabinetService {
    @Resource
    private CabinetMapper cabinetMapper;
    @Resource
    private OrderMapper orderMapper;

    @Override
    public Result findCabinetIdByCondition(Map<String, String> jsonBody) throws BindingException {
        String cabinet_province = jsonBody.get("cabinet_province");
        String cabinet_city = jsonBody.get("cabinet_city");
        String cabinet_county = jsonBody.get("cabinet_county");
        String cabinet_location_name = jsonBody.get("cabinet_location_name");
        String cabinet_name = jsonBody.get("cabinet_name");
        try {
            int cabinetId = cabinetMapper.findCabinetIdAllCondition(cabinet_province, cabinet_city, cabinet_county,
                    cabinet_location_name, cabinet_name);
            if (cabinetId >= 0) {
                return Result.success("外卖柜查询成功", cabinetId);
            }
        } catch (BindingException e) {
            return Result.fail("该外卖柜查询失败");
        }
        return Result.fail("外卖柜查询失败");
    }

    @Override
    public Result findCabinetSituation(int cabinetId) {
        //首先返回该用户的所有外卖所在的柜门集合
        List<Map<String, Object>> usedCabinetList = orderMapper.findUsedDoorByCabinetId(cabinetId);
        List<Map<String, Object>> overdueCabinetList = orderMapper.findOverdueDoorByCabinetId(cabinetId);

        Iterator<Map<String, Object>> usedCabinetIterator = usedCabinetList.iterator();
        Iterator<Map<String, Object>> overdueCabinetIterator = overdueCabinetList.iterator();
        //将过期的历史订单删除当前已存入新的外卖订单的历史订单
        while (overdueCabinetIterator.hasNext()) {
            Map<String, Object> overdueCabinet = overdueCabinetIterator.next();
            usedCabinetIterator = usedCabinetList.iterator();
            while (usedCabinetIterator.hasNext()) {
                Map<String, Object> usedCabinet = usedCabinetIterator.next();
                if (usedCabinet.get("order_doorNum").equals(overdueCabinet.get("order_doorNum"))) {
                    overdueCabinetIterator.remove();
                    break;
                }
            }
        }
        //生成空柜的柜门集合
        List<Map<String, Integer>> emptyCabinetList = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            usedCabinetIterator = usedCabinetList.iterator();
            overdueCabinetIterator = overdueCabinetList.iterator();
            boolean ifEmpty = true;
            while (overdueCabinetIterator.hasNext() && ifEmpty) {
                Map<String, Object> overdueCabinet = overdueCabinetIterator.next();
                if (Integer.parseInt(overdueCabinet.get("order_doorNum").toString()) == i) {
                    ifEmpty = false;
                    break;
                }
            }
            while (usedCabinetIterator.hasNext() && ifEmpty) {
                Map<String, Object> usedCabinet = usedCabinetIterator.next();
                if (Integer.parseInt(usedCabinet.get("order_doorNum").toString()) == i) {
                    ifEmpty = false;
                    break;
                }
            }
            if (ifEmpty) {
                Map<String, Integer> emptyMap = new HashMap<>();
                emptyMap.put("order_doorNum", i);
                emptyCabinetList.add(emptyMap);
            }
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("usedCabinetResult", usedCabinetList);
        resultMap.put("overdueCabinetResult", overdueCabinetList);
        resultMap.put("emptyCabinetResult", emptyCabinetList);

        return Result.success("获取柜门状态集合成功", resultMap);
    }
}
